package com.example.flutter_demo2.aty

import android.content.Intent
import android.os.Bundle
import androidx.annotation.NonNull
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.example.flutter_demo2.MyApplication
import com.example.flutter_demo2.R
import com.example.flutter_demo2.plugins.common.CommonPlugin
import com.example.flutter_demo2.utils.CommonUtil.setFullScreen
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.engine.FlutterEngineCache

class MainActivity : FragmentActivity(), OnBack2flutter, OnSwitch2Web {

    companion object {
        private const val TAG_FLUTTER_FRAGMENT = "flutter_fragment"
        private const val TAG_WEB_FRAGMENT = "web_fragment"
    }

    private lateinit var mFlutterFragment: FlutterFragment
    private lateinit var mFragmentManager: FragmentManager
    private lateinit var mWebViewFragment: Fragment
    private var mCurrentFragment = TAG_FLUTTER_FRAGMENT

    override fun onCreate(savedInstanceState: Bundle?) {
        // before super.onCreate.
        setFullScreen(this@MainActivity)
        MyApplication.splashScreen = installSplashScreen()
        MyApplication.splashScreen.setKeepOnScreenCondition { true }

        // plugin callback
        val engine = FlutterEngineCache.getInstance().get(MyApplication.fragmentEngineName)
        val commonPlugin: CommonPlugin? =
            engine?.plugins?.get(CommonPlugin::class.java) as CommonPlugin?
        commonPlugin?.setSwitch2WebCallback(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initFragment()
    }

    private fun initFragment() {
        mFragmentManager = supportFragmentManager
        mFlutterFragment =
            FlutterFragment.withCachedEngine(MyApplication.fragmentEngineName).build()
        mWebViewFragment = WebViewFragment(this@MainActivity)
        switchFragment(TAG_FLUTTER_FRAGMENT, mFlutterFragment)
    }

    private fun switchFragment(tag: String, fragment: Fragment) {
        mFragmentManager
            .beginTransaction()
            .replace(
                R.id.fragment_container,
                fragment,
                tag
            )
            .commit()
        mCurrentFragment = tag
    }

    override fun onBack2flutter() {
        switchFragment(TAG_FLUTTER_FRAGMENT, mFlutterFragment)
    }

    override fun onSwitch2Web() {
        switchFragment(TAG_WEB_FRAGMENT, mWebViewFragment)
    }


    // below, hole gesture, back press so on..
    override fun onPostResume() {
        super.onPostResume()
        mFlutterFragment.onPostResume()
    }

    override fun onNewIntent(@NonNull intent: Intent) {
        super.onNewIntent(intent)
        mFlutterFragment.onNewIntent(intent)
    }

    override fun onBackPressed() {
        if (TAG_WEB_FRAGMENT == mCurrentFragment) {
            switchFragment(TAG_FLUTTER_FRAGMENT, mFlutterFragment)
        } else {
            mFlutterFragment.onBackPressed()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        mFlutterFragment.onRequestPermissionsResult(
            requestCode,
            permissions,
            grantResults
        )
    }

    override fun onUserLeaveHint() {
        mFlutterFragment.onUserLeaveHint()
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        mFlutterFragment.onTrimMemory(level)
    }
}

interface OnSwitch2Web {
    fun onSwitch2Web()
}
